.TH std::set::emplace 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::set::emplace \- std::set::emplace

.SH Synopsis
   template< class... Args >                             \fI(since C++11)\fP
   std::pair<iterator, bool> emplace( Args&&... args );

   Inserts a new element into the container constructed in-place with the given args,
   if there is no element with the key in the container.

   The constructor of the new element is called with exactly the same arguments as
   supplied to emplace, forwarded via std::forward<Args>(args).... The element may be
   constructed even if there already is an element with the key in the container, in
   which case the newly constructed element will be destroyed immediately.

   Careful use of emplace allows the new element to be constructed while avoiding
   unnecessary copy or move operations.

   No iterators or references are invalidated.

.SH Parameters

   args - arguments to forward to the constructor of the element

.SH Return value

   A pair consisting of an iterator to the inserted element (or to the element that
   prevented the insertion) and a bool value set to true if and only if the insertion
   took place.

.SH Exceptions

   If an exception is thrown for any reason, this function has no effect (strong
   exception safety guarantee).

.SH Complexity

   Logarithmic in the size of the container.

.SH Example


// Run this code

 #include <chrono>
 #include <cstddef>
 #include <functional>
 #include <iomanip>
 #include <iostream>
 #include <string>
 #include <set>

 class Dew
 {
 private:
     int a, b, c;

 public:
     Dew(int _a, int _b, int _c)
         : a(_a), b(_b), c(_c)
     {}

     bool operator<(const Dew& other) const
     {
         return (a < other.a) ||
                (a == other.a && b < other.b) ||
                (a == other.a && b == other.b && c < other.c);
     }
 };

 constexpr int nof_operations{101};

 std::size_t set_emplace()
 {
     std::set<Dew> set;
     for (int i = 0; i < nof_operations; ++i)
         for (int j = 0; j < nof_operations; ++j)
             for (int k = 0; k < nof_operations; ++k)
                 set.emplace(i, j, k);

     return set.size();
 }

 std::size_t set_insert()
 {
     std::set<Dew> set;
     for (int i = 0; i < nof_operations; ++i)
         for (int j = 0; j < nof_operations; ++j)
             for (int k = 0; k < nof_operations; ++k)
                 set.insert(Dew(i, j, k));

     return set.size();
 }

 void time_it(std::function<int()> set_test, std::string what = "")
 {
     const auto start = std::chrono::system_clock::now();
     const auto the_size = set_test();
     const auto stop = std::chrono::system_clock::now();
     const std::chrono::duration<double, std::milli> time = stop - start;
     if (!what.empty() && the_size)
         std::cout << std::fixed << std::setprecision(2)
                   << time << " for " << what << '\\n';
 }

 int main()
 {
     time_it(set_insert, "cache warming...");
     time_it(set_insert, "insert");
     time_it(set_insert, "insert");
     time_it(set_emplace, "emplace");
     time_it(set_emplace, "emplace");
 }

.SH Possible output:

 630.58ms for cache warming...
 577.16ms for insert
 560.84ms for insert
 547.10ms for emplace
 549.44ms for emplace

.SH See also

   emplace_hint constructs elements in-place using a hint
   \fI(C++11)\fP      \fI(public member function)\fP
                inserts elements
   insert       or nodes
                \fI(since C++17)\fP
                \fI(public member function)\fP
